<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <script th:src="@{/layui/layui.js}"></script>
</head>
<body>
<table id="infoTable" lay-filter="infoTable"></table>
<script type="text/html" id="oprBtn">
    <a class="layui-btn layui-btn-xs" lay-event="edit">已读</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript" th:inline="none">
    layui.use(['table', 'jquery','layer'], function () {
        var table = layui.table,
            $ = layui.$,
        layer=layui.layer,
        infourl = "";
        if ($(parent.document).find("#usid").val()) {
            infourl = "/information/list/1/" + $(parent.document).find("#usid").val();
        } else {
            infourl = "/information/list/2/" + $(parent.document).find("#rsid").val();
        }
        table.render({
            id: 'infoTable'
            , elem: '#infoTable'
            , url: infourl
            , title: '用户数据表'
            , method: 'get'
            , cols: [[
                 {field: 'content', title: '内容', align: 'center',width: 180}
                , {field: 'recTime', title: '时间', align: 'center',width:160}
                , {
                    field: 'seen', title: '状态', align: 'center',width: 100, templet: function (res) {
                        return res.seen == 1 ? '已读' : '未读';
                    }
                }
                , { fixed: 'right', title: '操作', toolbar: '#oprBtn', align: 'center' }
            ]]
            , response: {
                statusCode: 200
            }
            , parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "data": res.data
                };
            }
        });
        table.on('tool(infoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除这条消息?', function (index) {
                    layer.close(index);
                    $.getJSON("/information/del/"+obj.data.id,function(result){
                        if(result.code==200){
                            layer.msg("删除成功")
                            obj.del()
                        }
                    })
                    console.log(obj.data.id)
                });
            } else if (obj.event === 'edit') {
                layer.confirm('标记已读?', function (index) {
                    layer.close(index);
                    $.getJSON("/information/read/"+obj.data.id,function(result){
                        if(result.code==200){
                            obj.update({
                                seen: '1'
                            });
                        }
                    })
                    console.log(obj.data.id)
                });
            }
        });
    })
</script>
</body>
</html>